package weaver.formmode.customjavacode.modeexpand.camci.utils;

import weaver.conn.RecordSet;
import weaver.formmode.setup.ModeRightInfo;
import weaver.general.TimeUtil;
import weaver.general.Util;

import java.util.*;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2022-07-06
 * @Description: ${description}
 * @ModifyLog:
 **/

public class CamciUtil {


    public static List<String> getListToReverseStr(String str) {
        List<String> list = new ArrayList<>();
        if (str.length() <= 4) {
            list.add( str );
        } else if (str.length() >= 4 && str.length() <= 8) {
            list.add( new StringBuilder( str.substring( 0, 4 ) ).reverse().toString() );
            list.add( new StringBuilder( str.substring( 4 ) ).reverse().toString() );
        } else if (str.length() >= 8 && str.length() <= 12) {
            list.add( new StringBuilder( str.substring( 0, 4 ) ).reverse().toString() );
            list.add( new StringBuilder( str.substring( 4, 8 ) ).reverse().toString() );
            list.add( new StringBuilder( str.substring( 8 ) ).reverse().toString() );
        } else if (str.length() >= 12 && str.length() <= 16) {
            list.add( new StringBuilder( str.substring( 0, 4 ) ).reverse().toString() );
            list.add( new StringBuilder( str.substring( 4, 8 ) ).reverse().toString() );
            list.add( new StringBuilder( str.substring( 8, 12 ) ).reverse().toString() );
            list.add( new StringBuilder( str.substring( 12 ) ).reverse().toString() );
        } else {
            list.add( new StringBuilder( str.substring( 0, 4 ) ).reverse().toString() );
            list.add( new StringBuilder( str.substring( 4, 8 ) ).reverse().toString() );
            list.add( new StringBuilder( str.substring( 8, 12 ) ).reverse().toString() );
            list.add( new StringBuilder( str.substring( 12, 16 ) ).reverse().toString() );
            list.add( new StringBuilder( str.substring( 16 ) ).reverse().toString() );
        }

        return list;
    }


    //将数据插入表单建模
    public static int insertModelData(int modeid, int userid, Map map) {
        int dataid = 0;
        RecordSet rs = new RecordSet();
        RecordSet rs1 = new RecordSet();
        //查询该模块表名
        String sql = "select b.tablename,a.formid from modeinfo a left join workflow_bill b on a.formid=b.id where a.id=?";
        rs.executeQuery( sql, modeid );
        rs.next();
        String tablename = Util.null2String( rs.getString( "tablename" ) );
        String uuid = UUID.randomUUID().toString();
        String insSql = "insert into " + tablename
                + "(uuid,modedatacreater,modedatacreatedate,modedatacreatetime,formmodeid)values(?,?,?,?,?)";
        Object[] params = {uuid, userid, TimeUtil.getCurrentDateString(), TimeUtil.getOnlyCurrentTimeString(), modeid};
        if (rs.executeUpdate( insSql, params )) {
            rs1.executeQuery( "select id from " + tablename + " where uuid=?", uuid );
            rs1.next();
            dataid = Util.getIntValue( rs1.getString( "id" ) );
            if (dataid > 0) {
                //遍历数据 进行update
                String updatesql = "update " + tablename + " set ";
                Set<String> keySet = map.keySet();
                for (String key : keySet) {
                    updatesql += key + "='" + map.get( key ).toString() + "',";
                }
                if (updatesql.endsWith( "," )) {
                    updatesql = updatesql.substring( 0, updatesql.length() - 1 );
                    updatesql += " where id=" + dataid;
                    rs.executeUpdate( updatesql );
                }
                //进行权限重构
                getNewModeRight( userid, modeid, dataid );
            }
        }
        return dataid;
    }

    /**
     * 更新数据
     *
     * @param modeid 表名
     * @param map    数据集合
     * @param dataid 更新用唯一标识
     * @return
     */
    public static boolean updateModelData(int modeid,int dataid, Map map) {
        RecordSet rs = new RecordSet();
        //查询该模块表名
        String sql1 = "select b.tablename,a.formid from modeinfo a left join workflow_bill b on a.formid=b.id where a.id=?";
        rs.executeQuery( sql1, modeid );
        rs.next();
        String tablename = Util.null2String( rs.getString( "tablename" ) );
        String sql = "update " + tablename + " set  ";
        String valuesStr = "";
        Iterator<Map.Entry<String, Object>> it = map.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<String, Object> entry = it.next();
            String key = Util.null2String( entry.getKey() );
            String value = Util.null2String( entry.getValue() );
            if (valuesStr.equals( "" )) {
                valuesStr = key + "=" + value + "";
            } else {
                valuesStr += "," + key + "=" + value + "";
            }
        }
        sql += valuesStr + " where id='" + dataid + "'";
        return rs.execute( sql );
    }

    public static boolean hasExistData(int dataid){
        RecordSet rs = new RecordSet(  );
        rs.executeQuery("select count(id) from uf_encryptioninfo where id=?",dataid);
        rs.next();
        int count = Util.getIntValue(rs.getString( 1 ));
        return count>0?true:false;
    }
    //建模权限重构
    public static void getNewModeRight(int userid, int formmodeid, int id) {
        ModeRightInfo modeRightInfo = new ModeRightInfo();
        modeRightInfo.setNewRight( true );
        modeRightInfo.editModeDataShare( userid, formmodeid, id );
    }
    //不够位数的在前面补0，保留num的长度位数字
    public static String autoGenericCode(String code, int num) {
        String result = "";
        result = String.format( "%0" + num + "d", Integer.parseInt( code ) );
        return result;
    }
   public static void main(String[] args) {
        Map<String, String> map = new HashMap<>();
      /*   String cjbm = "上海泛微网络科技股份有限公司";
        if (!"".equals( cjbm )) {
            List<String> list = CamciUtil.getListToReverseStr( cjbm );
            int count = 0;
            for (String str : list) {
                System.out.println( "customfield" + count + "=" + Util.null2String( str ) );
                map.put( "customfield" + count, Util.null2String( str ) );
                count++;
            }

        }
*/
        String sjh = "15094305633";
        if (!"".equals( sjh )) {
            List<String> list = CamciUtil.getListToReverseStr( sjh );
            int count = 0;
            for (String str : list) {
                Map<String, String> dataMap = CalUtil.divideAndRemainder( str, "3" );
                for (String key : dataMap.keySet()) {

                    String num = autoGenericCode(Util.null2String( key ),str.length());
                    System.out.println( "customfield" + count + "=" + num );
                    map.put( "customfield" + count, Util.null2String( key ) );
                    count++;
                    System.out.println( "customfield" + count + "=" + Util.null2String( dataMap.get( key ) ) );
                    map.put( "customfield" + count, Util.null2String( dataMap.get( key ) ) );
                    count++;
                }


            }
        }

       /* String skzh = "6214830106808297";
        if (!"".equals( skzh )) {
            List<String> list = CamciUtil.getListToReverseStr( skzh );
            int count = 15;
            for (String str : list) {
                Map<String, String> dataMap = CalUtil.divideAndRemainder( str, "3" );
                for (String key : dataMap.keySet()) {
                    System.out.println( "customfield" + count + "=" + Util.null2String( key ) );
                    map.put( "customfield" + count, Util.null2String( key ) );
                    count++;
                    System.out.println( "customfield" + count + "=" + Util.null2String( dataMap.get( key ) ) );
                    map.put( "customfield" + count, Util.null2String( dataMap.get( key ) ) );
                    count++;
                }
            }
        }*/

       /* String sfzh = "320922199208267115";
        if (!"".equals( sfzh )) {
            sfzh =  sfzh.contains( "X" )?sfzh.substring( 0,17 )+"10":sfzh;
            System.out.println( "sfzh" +sfzh );
            List<String> list = CamciUtil.getListToReverseStr( sfzh );
            int count = 25;
            for (String str : list) {
                System.out.println( "str" +str );
                Map<String, String> dataMap = CalUtil.divideAndRemainder( str, "3" );
                for (String key : dataMap.keySet()) {
                    System.out.println( "customfield" + count + "=" + Util.null2String( key ) );
                    map.put( "customfield" + count, Util.null2String( key ) );
                    count++;
                    System.out.println( "customfield" + count + "=" + Util.null2String( dataMap.get( key ) ) );
                    map.put( "customfield" + count, Util.null2String( dataMap.get( key ) ) );
                    count++;
                }
            }
        }*/
        //updateModelData( 1,12,map);
    }
}
